package com.lanyuan.a.controller;


import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanyuan.a.atest.BaseFormMap;
import com.lanyuan.a.bean.Comment;
import com.lanyuan.a.bean.User;
import com.lanyuan.a.bean.Window;
import com.lanyuan.a.bean.union.Profit;
import com.lanyuan.a.service.ButtonService;
import com.lanyuan.a.service.CommentService;
import com.lanyuan.a.service.FoodService;
import com.lanyuan.a.service.ProfitService;
import com.lanyuan.a.service.WindowFoodService;
import com.lanyuan.a.service.WindowService;
import com.lanyuan.a.service.union.CBSService;
import com.lanyuan.controller.index.BaseController;
import com.lanyuan.plugin.PageView;
import com.lanyuan.util.Common;

@Controller
@RequestMapping("/cbs/")
public class CBSController extends BaseController {
	@Autowired
	FoodService foodService;
	@Autowired
	ButtonService buttonService;
	@Resource
	CBSService cbsService;
	@Resource
	WindowService windowService;
	@Resource
	WindowFoodService windowFoodService;
	
	@Resource
	ProfitService profitService;
	@Resource
	CommentService commentService;
	
	@RequestMapping("list")
	public String listUI(Model model) throws Exception {
//		profitService.test();
		model.addAttribute("res", windowService.list());
		model.addAttribute("buttons", buttonService.getButtons("#food"));
		return Common.BACKGROUND_PATH + "/system/cbs/list";
	}

	@ResponseBody
	@RequestMapping("findByPage")
	public PageView findByPage(String pageNow,String pageSize) throws Exception {
		BaseFormMap baseFormMap = getFormMap(BaseFormMap.class);
		baseFormMap=toFormMap(baseFormMap, pageNow, pageSize);
//		List<Window> list = windowService.queryByPage(baseFormMap);
		List<Window> list = cbsService.queryWindowsByPage(baseFormMap);
		pageView.setRecords(list);//不调用默认分页,调用自已的mapper中findUserPage
        return pageView;
	}
	
	@RequestMapping("profit")
	public String profit(Model model) throws Exception {
		model.addAttribute("windows", windowService.list());
		return Common.BACKGROUND_PATH + "/system/cbs/profit";
	}
	
	@ResponseBody
	@RequestMapping("findProfitByPage")
	public PageView findProfitByPage(String pageNow,String pageSize) throws Exception {
		BaseFormMap baseFormMap = getFormMap(BaseFormMap.class);
		baseFormMap=toFormMap(baseFormMap, pageNow, pageSize);
		List list = profitService.profitList(baseFormMap);
		
		pageView.setRecords(list);//不调用默认分页,调用自已的mapper中findUserPage
        return pageView;
	}
	
	@RequestMapping("ckprofit")
	public String ckprofit(Model model,HttpServletRequest request) throws Exception {
		User user = (User)request.getSession().getAttribute("user");
		int cid = user.getId();
		model.addAttribute("windows", windowService.list(cid));
		return Common.BACKGROUND_PATH + "/system/cbs/ckprofit";
	}
	@ResponseBody
	@RequestMapping("findckProfitByPage")
	public PageView findckProfitByPage(String pageNow,String pageSize , Model model,HttpServletRequest request) throws Exception {
		BaseFormMap baseFormMap = getFormMap(BaseFormMap.class);
		baseFormMap=toFormMap(baseFormMap, pageNow, pageSize);
		User user = (User)request.getSession().getAttribute("user");
		int cid = user.getId();
		List list = profitService.ckprofitList(baseFormMap , cid);
		
		pageView.setRecords(list);//不调用默认分页,调用自已的mapper中findUserPage
        return pageView;
	}
	
	@RequestMapping("commentlist")
	public String commentlist(Model model) throws Exception {
		model.addAttribute("buttons", buttonService.getButtons("#comment"));
		return Common.BACKGROUND_PATH + "/system/cbs/commentlist";
	}
	@ResponseBody
	@RequestMapping("findcommentByPage")
	public PageView findcommentByPage(String pageNow,String pageSize , Model model,HttpServletRequest request) throws Exception {
		BaseFormMap baseFormMap = getFormMap(BaseFormMap.class);
		baseFormMap=toFormMap(baseFormMap, pageNow, pageSize);
		User user = (User)request.getSession().getAttribute("user");
		int cid = user.getId();
		List<Comment> list = cbsService.queryCommentByPage(baseFormMap , user);
		pageView.setRecords(list);//不调用默认分页,调用自已的mapper中findUserPage
        return pageView;
	}
	
	@ResponseBody
	@RequestMapping("deleteComment")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	public String deleteComment() throws Exception {
		String[] ids = getParaValues("ids");
		for(String a : ids){
			String[] arr = a.split(",");
			commentService.delByIds(arr);
		}
	
		return "success";
	}

}